package com.atguigu.srb.core.controller.admin;

import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.result.Result;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: AdminIntegralGradeController
 * Package: com.atguigu.srb.core.controller.admin
 * Description:
 *
 * @Author 王晓龙
 * @Create 2023/3/25 14:52
 * @Version 1.0
 */
@Api(tags = "积分等级管理")
@RestController
@RequestMapping("/admin/core/integralGrade")
@Slf4j
//@CrossOrigin
public class AdminIntegralGradeController {
    @Resource
    IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public Result list(){
        List<IntegralGrade> list = integralGradeService.list();
        return Result.ok().data("list", list);
    }

    @ApiOperation(value = "根据id删除积分等级", notes = "逻辑删除")
    @DeleteMapping("/remove/{id}")
    public Result removeById(
            @ApiParam(value = "数据id", required = true, example = "1")
            @PathVariable String id
    ){
        boolean result = integralGradeService.removeById(id);
        if (result){
            return Result.ok().message("删除成功");
        }else{
            return Result.error().message("删除失败");
        }
    }

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public Result save(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade
    ){
        Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean result = integralGradeService.save(integralGrade);
        if (result){
            return Result.ok().message("添加成功");
        }else{
            return Result.error().message("添加失败");
        }
    }

    @ApiOperation("根据id查询积分等级")
    @GetMapping("/get/{id}")
    public Result getById(
            @ApiParam(value = "数据id", required = true, example = "1")
            @PathVariable String id
    ){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (integralGrade != null){
            return Result.ok().data("data", integralGrade);
        }else{
            return Result.error().message("数据不存在");
        }
    }

    @ApiOperation("修改积分等级")
    @PutMapping("/update")
    public Result updateById(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade
    ){
        boolean result = integralGradeService.updateById(integralGrade);
        if (result){
            return Result.ok().message("修改成功");
        }else{
            return Result.error().message("修改失败");
        }
    }

    public void listAll(){
        //{} 代表占位符
        log.debug("{}级别日志" , "debug");
        log.info("{}级别日志" , "info");
        log.warn("{}级别日志" , "warn");
        log.error("{}级别日志" , "error");
    }
}